package servlet;

import util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/11 10:05
 * Version:1.0-SNAPSHOT
 * Description:
 */
@WebServlet("/showModifyEmp")
public class ShowModifyEmpServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        PrintWriter out = response.getWriter();

        // JDBC操作
        // 根据id查询员工信息
        // id是由员工列表页面传递过来的
        // ?key=value&key2=value2&key3=value3...
        String id = request.getParameter("id");
        System.out.println("当前设置默认值的员工id为:"+id);
        // 使用JDBC访问数据的方式将对应的员工信息插入到数据库中
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,"root","");
            String sql = new StringBuffer()
                    .append(" select id,name,salary,gender,edu,hobbies ")
                    .append(" from t_emp")
                    .append(" where id = ? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,Integer.parseInt(id));
            rs = ps.executeQuery();
            if(rs.next()){
              // 当查询到对应信息之后,绘制修改页面
                out.print(" <!DOCTYPE html> ");
                out.print(" <html lang=\"en\"> ");
                out.print(" <head> ");
                out.print("     <meta charset=\"UTF-8\"> ");
                out.print("     <title>修改员工信息</title> ");
                out.print(" </head> ");
                out.print(" <body> ");
                out.print(" <h1>员工修改</h1> ");
                out.print(" <a href=\"findAll\">返回员工列表</a> ");
                out.print(" <hr/> ");
                out.print(" <form action=\"modifyEmp\" method=\"post\"> ");
                out.print("     编号:<input type=\"text\" name=\"id\" value=\""+rs.getInt("id")+"\" readonly/><br/> ");
                out.print("     姓名:<input type=\"text\" name=\"name\" value=\""+rs.getString("name")+"\"/><br/> ");
                out.print("     工资:<input type=\"text\" name=\"salary\" value=\""+rs.getDouble("salary")+"\"/><br/> ");
                out.print("     性别: ");
                out.print("     <input type=\"radio\" name=\"gender\" value=\"man\" "+("man".equals(rs.getString("gender")) ? "checked" : "")+">男 ");
                out.print("     <input type=\"radio\" name=\"gender\" value=\"woman\" "+("woman".equals(rs.getString("gender")) ? "checked" : "")+">女<br/> ");
                out.print("     学历: ");
                out.print("     <select name=\"edu\"> ");
                out.print("         <option value=\"-1\">请选择</option> ");
                out.print("         <option value=\"1\" "+("1".equals(rs.getString("edu")) ? "selected" : "")+">大专</option> ");
                out.print("         <option value=\"2\" "+("2".equals(rs.getString("edu")) ? "selected" : "")+">本科</option> ");
                out.print("         <option value=\"3\" "+("3".equals(rs.getString("edu")) ? "selected" : "")+">研究生</option> ");
                out.print("         <option value=\"4\" "+("4".equals(rs.getString("edu")) ? "selected" : "")+">博士生</option> ");
                out.print("     </select><br/> ");
                out.print("     兴趣: ");
                // 数据库中查询出来的是一个字符串
                // 此处是数组
                // 需要先将字符串分隔成原始数组,然后判断是否包含对应的元素
                out.print("     <input type=\"checkbox\" name=\"hobbies\" value=\"1\" "+ StringUtil.checkHobby(rs.getString("hobbies"),"1") +">吃饭 ");
                out.print("     <input type=\"checkbox\" name=\"hobbies\" value=\"2\" "+ StringUtil.checkHobby(rs.getString("hobbies"),"2") +">睡觉 ");
                out.print("     <input type=\"checkbox\" name=\"hobbies\" value=\"3\" "+ StringUtil.checkHobby(rs.getString("hobbies"),"3") +">打豆豆<br/> ");
                out.print("     <input type=\"submit\" value=\"修改\"> ");
                out.print(" </form> ");
                out.print(" </body> ");
                out.print(" </html> ");
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
